/*
 * Copyright (c) 2014 ICM Uniwersytet Warszawski All rights reserved.
 * See LICENCE.txt file for licensing information.
 */
package pl.edu.icm.unity.saml.metadata;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import pl.edu.icm.unity.server.utils.Log;

import xmlbeans.org.oasis.saml2.metadata.EntityDescriptorDocument;

/**
 * A very simple servlet: returns SAML metadata generated by a given {@link MetadataProvider}.
 * 
 * @author K. Benedyczak
 */
public class MetadataServlet extends HttpServlet
{
	private static Logger log = Log.getLogger(Log.U_SERVER_SAML, MetadataServlet.class);
	private MetadataProvider metaProvider;
	
	public MetadataServlet(MetadataProvider metaProvider)
	{
		this.metaProvider = metaProvider;
	}
	
	protected MetadataServlet()
	{
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		serveMetadata(metaProvider, req, resp);
	}
	
	protected void serveMetadata(MetadataProvider metaProvider,
			HttpServletRequest req, HttpServletResponse resp) throws IOException
	{
		resp.setContentType("application/samlmetadata+xml");
		EntityDescriptorDocument meta;
		try
		{
			meta = metaProvider.getMetadata();
		} catch (Exception e)
		{
			log.error("Problem generating SAML metadata document.", e);
			resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 
					"Problem occured when generating metadata.");
			return;
		}
		PrintWriter writer = resp.getWriter();
		writer.write(meta.xmlText());
		writer.flush();
	}
}
